package com.qyhl.module_practice.newhome.fragment.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.news.NewsBean;
import com.qyhl.webtv.commonlib.utils.eventbus.BusFactory;
import com.qyhl.webtv.commonlib.utils.eventbus.Event;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class PracticeHomeNewsFragment extends BaseFragment implements PracticeHomeNewsContract.PracticeHomeNewsView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;


    private String sectionId;

    private CommonAdapter<NewsBean> commonAdapter;

    private List<NewsBean> mDataList = new ArrayList<>();

    private PracticeHomeNewsPresenter mPresenter;


    public void setSectionId(String sectionId) {
        this.sectionId = sectionId;
    }

    public static PracticeHomeNewsFragment newInstance(String sectionId) {
        PracticeHomeNewsFragment fragment = new PracticeHomeNewsFragment();
        fragment.setSectionId(sectionId);
        return fragment;
    }

    @Override
    public void initImmersionBar() {

    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.practice_fragment_home_news, null);
    }

    @Override
    protected void create() {
        mPresenter = new PracticeHomeNewsPresenter(this);
        BusFactory.getBus().register(this);
        loadMask.setStatus(LoadingLayout.Loading);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.practice_item_news_divider));
        recycleView.addItemDecoration(divider);
        recycleView.setAdapter(commonAdapter = new CommonAdapter<NewsBean>(getContext(), R.layout.practice_item_news, mDataList) {
            @Override
            protected void convert(ViewHolder holder, NewsBean bean, int position) {
                holder.setText(R.id.title, bean.getTitle());
                if (StringUtils.isNotEmpty(bean.getSource())) {
                    holder.setVisible(R.id.source, true);
                    holder.setText(R.id.source, bean.getSource());
                } else {
                    holder.setVisible(R.id.source, false);
                }

                if (StringUtils.isNotEmpty(bean.getHitCount() + "")) {
                    holder.setText(R.id.scan_num, bean.getHitCount() + " 阅");
                } else {
                    holder.setText(R.id.scan_num, "0 阅");
                }

                holder.setText(R.id.comment_num, bean.getCommentCount() + " 评");

                holder.setVisible(R.id.comment_num, false);

                holder.setVisible(R.id.scan_num, false);

                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(PracticeHomeNewsFragment.this)
                        .load(bean.getLogo())
                        .apply(new RequestOptions().error(R.drawable.cover_large_default).placeholder(R.drawable.cover_large_default))
                        .into(cover);
            }
        });

        mPresenter.getData(sectionId);
    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                mPresenter.getData(sectionId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                NewsBean bean = mDataList.get(position);
                switch (mDataList.get(position).getType()) {
                    case "1"://普通新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.NORMAL_NEWS_ACTIVITY, bundle);
                    }
                    break;
                    case "2"://图片新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        bundle.putString("section", bean.getCatalogID());
                        RouterManager.router(ARouterPathConstant.PICTURE_NEWS_ACTIVITY, bundle);
                    }

                    break;
                    case "3"://直播
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("newsId", bean.getID());
                        bundle.putString("liveType", bean.getLivetype());
                        RouterManager.router(ARouterPathConstant.LIVE_NORMAL_ACTIVITY, bundle);
                    }
                    break;
                    case "4"://标题新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        bundle.putString("title", bean.getTitle());
                        bundle.putString("url", bean.getRedirectURL());
                        RouterManager.router(ARouterPathConstant.ADV, bundle);
                    }
                    break;
                    case "5"://视频新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.VIDEO_NEWS_ACTIVITY, bundle);
                    }
                    break;
                    case "8"://互动直播
                        break;
                    case "101"://抓取新闻
                    {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", bean.getID());
                        RouterManager.router(ARouterPathConstant.NEWS_CATCH, bundle);
                    }
                    break;
                    default:
                        break;

                }
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


    @Override
    public void setData(List<NewsBean> beanList) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");

        mDataList.clear();
        if (beanList.size() > 5) {
            List<NewsBean> list = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                list.add(beanList.get(i));
            }
            mDataList.addAll(list);
        } else {
            mDataList.addAll(beanList);
        }

        if (commonAdapter != null) {
            commonAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void setError(String msg) {
        loadMask.setStatus(LoadingLayout.Error);
        loadMask.setReloadButtonText("点击重试~");
        if (!NetUtil.isNetworkAvalible(getContext())) {
            loadMask.setErrorImage(R.drawable.error_network);
            loadMask.setErrorText("网络异常，请检查您的网络！");
        } else if (msg.contains("暂无")) {
            loadMask.setErrorImage(R.drawable.empty_content);
            loadMask.setErrorText(msg);
        } else {
            loadMask.setErrorImage(R.drawable.error_content);
            loadMask.setErrorText(msg);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(Event.PracticeHomeNewsRefresh event) {
        if (event != null) {
            sectionId = event.getSectionId();
            mPresenter.getData(sectionId);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        BusFactory.getBus().unregister(this);
    }


}
